package com.dabai.simpleconnection;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;

/**
 * Description : Other
 *
 * @author BAI
 */

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

import com.dabai.dbutils.utils.DabaiUtils;

/**
 * 类说明：
 *
 * @author fuyanan
 * @date 2015-8-28
 * @version 1.0.0
 */
public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        SmsMessage msg = null;
        if (null != bundle) {
            Object[] smsObj = (Object[]) bundle.get("pdus");
            for (Object object : smsObj) {
                msg = SmsMessage.createFromPdu((byte[]) object);
                Date date = new Date(msg.getTimestampMillis());// 时间
                @SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat(
                        "yyyy-MM-dd HH:mm:ss");
                String receiveTime = format.format(date);

                String sendMsg = "发信人:" + msg.getOriginatingAddress()
                        + "\n内容:" + msg.getDisplayMessageBody() + "\n时间:"
                        + msg.getTimestampMillis();

                Intent i = new Intent("sendMsg");
                i.putExtra("msg", sendMsg); //EDIT: this passes a parameter to the receiver
                context.sendBroadcast(i);

                DabaiUtils.showDialog(context, msg.getDisplayMessageBody());

            }
        }
    }
}